package model

import (
	"gorm.io/gorm"
	__ "grpc/basic/proto"
)

// 历史记录
type History struct {
	gorm.Model
	VideoId    int    `gorm:"type:int" json:"videoId"`
	Season     string `gorm:"type:varchar(10)" json:"season"`     //季
	Collection string `gorm:"type:varchar(10)" json:"collection"` //集
	Watch      int    `gorm:"type:int" json:"watch"`              //观看时长
}

func (h *History) AddHistory(db *gorm.DB) error {
	return db.Debug().Create(&h).Error
}

func (h *History) ListHistory(db *gorm.DB, page, size int64) (list []*__.ListHistory, err error) {
	if page <= 0 {
		page = 1
	}

	if size <= 0 || size >= 20 {
		size = 10
	}

	offset := (page - 1) * size
	err = db.Debug().Model(&History{}).
		Select("histories.id as historyId", "videos.title as title",
			"videos.content as content", "histories.season as season", "histories.collection as collection",
			"histories.watch as watch").
		Joins("left join videos on videos.id = histories.video_id").Offset(int(offset)).
		Limit(int(size)).Order("histories.created_at  desc").
		Find(&list).Error
	return list, err
}
